package com.jt.assistant.repository

import com.jt.assistant.api.ApiService
import com.jt.assistant.base.BaseResult
import com.jt.assistant.bean.*
import com.jt.assistant.http.Authorization
import com.jt.assistant.http.RetrofitClient
import com.tencent.mmkv.MMKV
import com.yzp.mvvmlibrary.base.BaseModel
import okhttp3.MediaType
import okhttp3.RequestBody
import java.io.File
import java.util.*

class HomeRepository private constructor(
) : BaseModel() {

    private val mService by lazy { RetrofitClient.getInstance().create(ApiService::class.java) }

    suspend fun login(param: Param): BaseResult<LoginDataBean> {
        return mService.login(
            Authorization.getInstance().authorization,
            param
        )
    }

    suspend fun getScanList(param: Map<String, String>): BaseResult<ScanDataBean> {
        return mService.getScanList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }

    suspend fun sendScanLog(param: MutableList<ScanCarton>): BaseResult<Any> {
        return mService.sendScanLog(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }

    suspend fun oneKeyScan(param: MutableList<OnKeyScanCarton>): BaseResult<Any> {
        return mService.oneKeyScan(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }
    suspend fun onekeyScanQuery(param: MutableList<OnKeyScanCarton>): BaseResult<Boolean> {
        return mService.onekeyScanQuery(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun modifyWaybill(param: Param): BaseResult<Any> {
        return mService.modifyWaybill(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getDeliverTruck(): BaseResult<MutableList<AddCarSearchBean>> {
        return mService.getDeliverTruck(
            Authorization.getInstance().authorization,
            getToken(),
        )
    }


    suspend fun getCarList(param: Param): BaseResult<CarSearchBean> {
        return mService.getCarList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getCarPlanList(param: Param): BaseResult<CarSelfBean> {
        return mService.getCarPlanList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getDic(param: Param): BaseResult<MutableList<DicBean>> {
        return mService.getDic(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun addDeliverCar(param: Param): BaseResult<Any> {
        return mService.addDeliverCar(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun generateReceipt(param: Param): BaseResult<Any> {
        return mService.generateReceipt(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getStoreCityList(): BaseResult<MutableList<CityBean>> {
        return mService.getStoreCityList(
            Authorization.getInstance().authorization,
            getToken()
        )
    }


    suspend fun getForwarderWaybillList(param: Map<String, String>): BaseResult<Any> {
        return mService.getForwarderWaybillList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun uploadForwarderWaybill(
        param: Param,
        pictureList: MutableList<String>
    ): BaseResult<Any> {
        val map: MutableMap<String, RequestBody> = HashMap()
        for (picturePath in pictureList) {
            if (picturePath.isNotEmpty()){
                val file = File(picturePath)
                map["image\"; filename=\"" + file.name] =
                    RequestBody.create(MediaType.parse("image/png"), file)
            }
        }
        return mService.uploadForwarderWaybill(
            Authorization.getInstance().authorization,
            getToken(),
            param,
            map
        )
    }


    suspend fun getWaybillSkuList(param: Map<String, String>): BaseResult<Any> {
        return mService.getWaybillSkuList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun uploadJdReceiveReport(
        param: Param,
        pictureList: MutableList<String>
    ): BaseResult<Any> {
        val map: MutableMap<String, RequestBody> = HashMap()
        for (picturePath in pictureList) {
            if (picturePath.isNotEmpty()){
                val file = File(picturePath)
                map["image\"; filename=\"" + file.name] =
                    RequestBody.create(MediaType.parse("image/png"), file)
            }
        }

        return mService.uploadJdReceiveReport(
            Authorization.getInstance().authorization,
            getToken(),
            param,
            map
        )
    }


    suspend fun getIntransitTruck(param: Param): BaseResult<MutableList<CarReceiveBean>> {
        return mService.getIntransitTruck(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun selfSendPlan(param: Param): BaseResult<MutableList<DeliveryBean>> {
        return mService.selfSendPlan(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }

    suspend fun getIntransitTruckDetail(param: Param): BaseResult<MutableList<CarReceiveDetailBean>> {
        return mService.getIntransitTruckDetail(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }

    suspend fun deliveryReceiptCreate(param: Param): BaseResult<DocumentDetail> {
        return mService.deliveryReceiptCreate(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun uploadDriverDeliveryReceipt(
        param: Param,
        pictureList: MutableList<String>
    ): BaseResult<Any> {
        val map: MutableMap<String, RequestBody> = HashMap()
        for (picturePath in pictureList) {
            if (picturePath.isNotEmpty()){
                val file = File(picturePath)
                map["image\"; filename=\"" + file.name] =
                    RequestBody.create(MediaType.parse("image/png"), file)
            }
        }
        return mService.uploadDriverDeliveryReceipt(
            Authorization.getInstance().authorization,
            getToken(),
            param,
            map
        )
    }


    suspend fun uploadDriverDeliveryImgUp(
        param: Param,
        pictureList: MutableList<String>
    ): BaseResult<Any> {
        val map: MutableMap<String, RequestBody> = HashMap()
        for (picturePath in pictureList) {
            if (picturePath.isNotEmpty()){
                val file = File(picturePath)
                map["image\"; filename=\"" + file.name] =
                    RequestBody.create(MediaType.parse("image/png"), file)
            }
        }
        return mService.uploadDriverDeliveryImgUp(
            Authorization.getInstance().authorization,
            getToken(),
            param,
            map
        )
    }


    suspend fun addHandingFee(
        param: Param,
        pictureList: MutableList<String>
    ): BaseResult<Any> {
        val map: MutableMap<String, RequestBody> = HashMap()
        for (picturePath in pictureList) {
            if (picturePath.isNotEmpty()){
                val file = File(picturePath)
                map["image\"; filename=\"" + file.name] =
                    RequestBody.create(MediaType.parse("image/png"), file)
            }
        }
        return mService.addHandingFee(
            Authorization.getInstance().authorization,
            getToken(),
            param,
            map
        )
    }


    suspend fun uploadReturnsStoreDeliveryReceipt(
        param: Param,
        pictureList: MutableList<String>
    ): BaseResult<Any> {
        val map: MutableMap<String, RequestBody> = HashMap()
        for (picturePath in pictureList) {
            if (picturePath.isNotEmpty()){
                val file = File(picturePath)
                map["image\"; filename=\"" + file.name] =
                    RequestBody.create(MediaType.parse("image/png"), file)
            }
        }
        return mService.uploadReturnsStoreDeliveryReceipt(
            Authorization.getInstance().authorization,
            getToken(),
            param,
            map
        )
    }


    suspend fun getReturnsSkuListByWaybill(param: Param): BaseResult<ReturnSkuBean> {
        return mService.getReturnsSkuListByWaybill(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun waybillDetail(param: Param): BaseResult<List<HandingFeeWaybillList>> {
        return mService.waybillDetail(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getSkuListByWaybill(param: Param): BaseResult<MutableList<SkuListBean>> {
        return mService.getSkuListByWaybill(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun sendReturnsPackingList(param: Param): BaseResult<CartonResponseBean> {
        return mService.sendReturnsPackingList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun modifyReturnsWaybill(param: Param): BaseResult<Any> {
        return mService.modifyReturnsWaybill(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getReturnsScanList(param: Map<String, String>): BaseResult<ScanDataBean> {
        return mService.getReturnsScanList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun sendReturnsScanLog(param: MutableList<ScanCarton>): BaseResult<Any> {
        return mService.sendReturnsScanLog(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getPackingSkuList(param: Map<String, String>): BaseResult<Any> {
        return mService.getPackingSkuList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun checkUpgrade(param: Param): BaseResult<Any> {
        return mService.checkUpgrade(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun forwarderList(): BaseResult<MutableList<ForwarderListBean>> {
        return mService.forwarderList(
            Authorization.getInstance().authorization,
            getToken(),
        )
    }


    suspend fun queryWaybillByCity(param: Param): BaseResult<MutableList<WaybillCityBean>> {
        return mService.queryWaybillByCity(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getReturnsCartonDetail(param: Param): BaseResult<CartonResponseBean> {
        return mService.getReturnsCartonDetail(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getSkuByChannelOrder(param: Param): BaseResult<MutableList<SkuBean>> {
        return mService.getSkuByChannelOrder(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getWareFileBySku(param: Param): BaseResult<MutableList<String>> {
        return mService.getWareFileBySku(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getEntrustBookByCartonCode(param: Param): BaseResult<EntrustBean> {
        return mService.getEntrustBookByCartonCode(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun wareFile(
        param: Param,
        picList: MutableList<String>
    ): BaseResult<Any> {
        val map: MutableMap<String, RequestBody> = HashMap()
        for (picturePath in picList) {
            if (picturePath.isNotEmpty()){
                val file = File(picturePath)
                map["image\"; filename=\"" + file.name] =
                    RequestBody.create(MediaType.parse("image/png"), file)
            }
        }
        return mService.wareFile(
            Authorization.getInstance().authorization,
            getToken(),
            param,
            map
        )
    }


    suspend fun getCityNameList(): BaseResult<CarCityBean> {
        return mService.getCityNameList(
            Authorization.getInstance().authorization,
            getToken(),
        )
    }


    suspend fun relationCity(param: Param): BaseResult<Any> {
        return mService.relationCity(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    suspend fun getNoCheckWaybillCarList(): BaseResult<CarViewBean> {
        return mService.getNoCheckWaybillCarList(
            Authorization.getInstance().authorization,
            getToken(),
        )
    }


    suspend fun carWaybillList(param: Param): BaseResult<CarViewDetailBean> {
        return mService.carWaybillList(
            Authorization.getInstance().authorization,
            getToken(),
            param
        )
    }


    companion object {
        @Volatile
        private var INSTANCE: HomeRepository? = null
        fun getInstance() =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?: HomeRepository().also { INSTANCE = it }
            }
    }


    private fun getToken(): String {
        return if (MMKV.defaultMMKV().getString("TOKEN", "").isNullOrEmpty()) {
            ""
        } else {
            MMKV.defaultMMKV().getString("TOKEN", "").toString()
        }
    }

}